package com.shuyou;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.shuyou.net.UserInfo;
import com.shuyou.values.Values;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

public class UserInfoActivity extends Activity implements OnClickListener{
	
	Dialog dialogUsrInfo = null;
	
	private TextView textNickName = null;
	private TextView textSex = null;
	private TextView textPhoneNum = null;
	private TextView textQQ = null;
	private TextView textWechat = null;
	
	private EditText editNickName = null;
	private EditText editPhoneNum = null;
	private EditText editQQ       = null;
	private EditText editWechat   = null;
	private RadioButton boy = null;
	private RadioButton girl = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_userinfo);
		findViewById(R.id.userinfo_back).setOnClickListener(this);
		findViewById(R.id.userinfo_usrlogo).setOnClickListener(this);
		findViewById(R.id.userinfo_nickname).setOnClickListener(this);
		findViewById(R.id.userinfo_sex).setOnClickListener(this);
		findViewById(R.id.userinfo_age).setOnClickListener(this);
		findViewById(R.id.userinfo_phonenum).setOnClickListener(this);
		findViewById(R.id.userinfo_qq).setOnClickListener(this);
		findViewById(R.id.userinfo_wechat).setOnClickListener(this);
		findViewById(R.id.userinfo_mybooks).setOnClickListener(this);
		
		InitDisplay();
	}
	
	/**
	 * 根据获得的第三方数据，初始化显示
	 */
	private void InitDisplay(){
		textNickName = (TextView)findViewById(R.id.userinfo_con_nickname);
		textSex = (TextView)findViewById(R.id.userinfo_con_sex);
		textPhoneNum = (TextView)findViewById(R.id.userinfo_con_phonenum);
		textQQ = (TextView)findViewById(R.id.userinfo_con_qq);
		textWechat = (TextView)findViewById(R.id.userinfo_con_wechat);
		
		textNickName.setText(UserInfo.nickName);
		textPhoneNum.setText(UserInfo.phoneNum);
		textQQ.setText(UserInfo.QQ);
		textWechat.setText(UserInfo.weChat);
		
		if(UserInfo.sex.equals("1"))
		{
			textSex.setText(Values.MALE);
		}
		else
		{
			textSex.setText(Values.FEMALE);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
			case R.id.userinfo_usrlogo: 
				showInfoDialog(R.layout.shuyou_dialog_userinfo_logo, 1);
				break;
			case R.id.userinfo_nickname: 
				showInfoDialog(R.layout.shuyou_dialog_userinfo_nickname, 2);
				
				break;
			case R.id.userinfo_sex: 
				showInfoDialog(R.layout.shuyou_dialog_userinfo_sex, 3);
				break;
			case R.id.userinfo_age: 
				showInfoDialog(R.layout.shuyou_dialog_userinfo_age, 4);
				break;
			case R.id.userinfo_phonenum: 
				showInfoDialog(R.layout.shuyou_dialog_userinfo_phonenum, 5);
				
				break;
			case R.id.userinfo_qq: 
				showInfoDialog(R.layout.shuyou_dialog_userinfo_qq, 6);
				break;
			case R.id.userinfo_wechat: 
				showInfoDialog(R.layout.shuyou_dialog_userinfo_wechat, 7);
				break;
			
			case R.id.dialog_nickname_confirm_button:
				textNickName.setText(editNickName.getText().toString());
				dialogUsrInfo.dismiss();
				break;
			case R.id.dialog_sex_confirm_button:
				if(boy.isChecked()){
					textSex.setText(Values.MALE);
				}
				else{
					textSex.setText(Values.FEMALE);
				}
				dialogUsrInfo.dismiss();
				break;
			case R.id.dialog_phonenum_confirm_button:
				String tmpPhoneNum = editPhoneNum.getText().toString();
				/*检测电话号码的正确性*/
				Pattern p = Pattern.compile
						("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
				Matcher m = p.matcher(tmpPhoneNum);
				if(m.matches()) {
					textPhoneNum.setText(tmpPhoneNum);
					dialogUsrInfo.dismiss();
				}
				else {
					final TextView tmpTv = (TextView)dialogUsrInfo.findViewById(R.id.dialog_phonenum_title);
					tmpTv.setText("号码有误");
					tmpTv.setTextColor(getResources().getColor(R.color.red));
					new Handler().postDelayed(new Runnable() {

			            @Override
			            public void run() {
			             tmpTv.setText(getResources().getString(R.string.phonenum_setting));
			             tmpTv.setTextColor(getResources().getColor(R.color.black));
			            }
			        }, 1000);     //1秒后修改回来
				}
				break;
			case R.id.dialog_qq_confirm_button:
				textQQ.setText(editQQ.getText().toString());
				dialogUsrInfo.dismiss();
				break;
			case R.id.dialog_wechat_confirm_button:
				textWechat.setText(editWechat.getText().toString());
				dialogUsrInfo.dismiss();
				break;
			case R.id.userinfo_back: 
				finish();
				break;
			case R.id.userinfo_mybooks:
				startActivity(new Intent(UserInfoActivity.this, BookshelfActivity.class));
				break;
			default: break;
		}
	}
	
	/**
	 * 根据id弹出相应窗口
	 * @param id
	 */
	private void showInfoDialog(int id, int whichInfo){
		LinearLayout dialogUsrInfoView = (LinearLayout)getLayoutInflater()
				.inflate(id, null);
		dialogUsrInfo = new Dialog(this, R.style.ShuyouDialog);
		dialogUsrInfo.setContentView(dialogUsrInfoView);
		dialogUsrInfo.show();
		switch(whichInfo){
			case 1: break;
			case 2:
				editNickName = (EditText)dialogUsrInfo.findViewById(R.id.dialog_nickname_setting);
				editNickName.setText(textNickName.getText().toString());
				dialogUsrInfo.findViewById(R.id.dialog_nickname_confirm_button).setOnClickListener(this);
				break;
			case 3:
				
				boy = (RadioButton)dialogUsrInfo.findViewById(R.id.dialog_userinfo_sex_boy);
				girl = (RadioButton)dialogUsrInfo.findViewById(R.id.dialog_userinfo_sex_girl);
				
				if(textSex.getText().toString() == Values.FEMALE){
					girl.setChecked(true);
				}
				else{
					boy.setChecked(true);
				}
				dialogUsrInfo.findViewById(R.id.dialog_sex_confirm_button).setOnClickListener(this);
				break;
			case 4:
				
				break;
			case 5:
				editPhoneNum = (EditText)dialogUsrInfo.findViewById(R.id.dialog_phonenum_setting);
				editPhoneNum.setText(textPhoneNum.getText().toString());
				dialogUsrInfo.findViewById(R.id.dialog_phonenum_confirm_button).setOnClickListener(this);
				break;
			case 6:
				editQQ = (EditText)dialogUsrInfo.findViewById(R.id.dialog_qq_setting);
				editQQ.setText(textQQ.getText().toString());
				dialogUsrInfo.findViewById(R.id.dialog_qq_confirm_button).setOnClickListener(this);
				break;
			case 7:
				editWechat = (EditText)dialogUsrInfo.findViewById(R.id.dialog_wechat_setting);
				editWechat.setText(textWechat.getText().toString());
				dialogUsrInfo.findViewById(R.id.dialog_wechat_confirm_button).setOnClickListener(this);
				break;
			default:
				break;	
		}
	}
}
